﻿@using Topro.Entity.Enum;
<div class="easyui-layout" data-options="fit: true">
    <div data-options="region: 'center', border: false" style="padding:10px; overflow: hidden;">
        <div class="easyui-layout" data-options="fit: true">
            <div data-options="region:'north',border: false,split:true" style="height:360px;">
                <div class="data-wrap case">
                    <div class="data-tools clearfix">
                        <div class="pull-left form-inline ">
                        </div>
                        <div class="pull-right">
                                <a class="btn btn-default btn-sm" onclick="return page.appendRow();"><span class="glyphicon glyphicon-plus"></span> 新增一行</a>
                        </div>
                    </div>
                    <form class="form-horizontal" id="form" onsubmit="return false">
                        <div class="tab-pane fade in active" id="home">
                            <div class="form-group">
                                <label class="wd100 control-label"><span class="WdateFmtErr">*</span>单据编号：</label>
                                <div class="wd400 m15_l">
                                    <input class="form-control input-xs" name="orderId" type="text" value="@(ViewBag.NextId)" data-options="required:true,prompt:'单据编号'">
                                </div>
                            </div>
                            <div class="form-group" >
                                <label class="wd100 control-label"><span class="WdateFmtErr">*</span>托盘编码：</label>
                                <div class="wd400 m15_l">
                                    <input class="form-control" type="text" name="containerCode" maxlength="5" autocomplete="off" data-options="prompt:'托盘编码'">
                                </div>
                            </div>
                            <div class="form-group" >
                                <label class="wd100 control-label">是否空托：</label>
                                <div class="wd400 m15_l">
                                    <input type="checkbox" name="isContainer" autocomplete="off">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="wd100 control-label"><span class="WdateFmtErr">*</span>物料高度(cm)：</label>
                                <div class="wd400 m15_l">
                                    <select name="type" class="form-control" >
                                        <option value="">请选择物料高度</option>
                                        <option value="10">500cm</option>
                                        <option value="20">1000cm</option>
                                        <option value="30">1200cm</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="wd100 control-label m15_l">入库库位：</label>
                                <div class="input-group wd400 m15_l">
                                    <input type="hidden" class="form-control" name="warehouseUnitId" value="" />
                                    <input type="text" class="form-control" name="warehouseUnitCode" maxlength="20" placeholder="入库库位" autocomplete="off" disabled />
                                    <span class="input-group-addon" style="margin-left:20px;">
                                        <i data-href="/Module/WarehouseUnit" data-title="入库库位" data-width="800" data-height="570" class="glyphicon glyphicon glyphicon-align-justify warehouseUnit"></i>
                                    </span>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="wd100 control-label"><span class="WdateFmtErr">*</span>站台：</label>
                                <div class="wd400 m15_l">
                                    <select name="stationNo" class="form-control" >
                                        <option value="">请选择站台</option>
                                        <option value="1">1->D14a入口</option>
                                        <option value="2">2->D14入口</option>
                                        <option value="4">4->D1A入口</option>
                                        <option value="6">6->D10入口</option>
                                        <option value="8">8->D1入口</option>
                                        <option value="10">10->D12入口</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group" >
                                <label class="wd100 control-label">备注：</label>
                                <div class="wd400 m15_l">
                                    <input class="form-control" type="text" name="remark" maxlength="50" autocomplete="off" data-options="prompt:'备注'">
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
            <div data-options="region:'center', border: false">
                <!-- 表格数据 -->
                <table id="productItemDG"></table>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">

    var productItemDG = $('#productItemDG');

    function Tip_success(word) {
        Topro.UI.Tip_success(word);
    }

    function set(obj) {
        $("input[name='warehouseUnitId']").val(obj.warehouseUnitId);
        $("input[name='warehouseUnitCode']").val(obj.warehouseUnitCode);
    }

    function save() {
        page.save();
    }

    var page = {
        view: function(obj, url) {
            var confirmname = "保存";

            if ($(obj).data("confirmname") != undefined && $(obj).data("confirmname") != "") {
                confirmname = $(obj).data("confirmname");
            }

            var initFun = function() {
                this.button(
                    {
                        name: confirmname,
                        callback: function() {
                            var iframe = this.iframe.contentWindow;
                            if (!iframe.document.body) {
                                return false;
                            }
                            iframe.save();
                            return false;
                        },
                        focus: true,
                    },
                    {
                        name: '取消'
                    }
                );
            };

            if ($(obj).data("bottom") == "remove") {
                initFun = function() { };
            }

            Topro.UI.DialogOpen(url, {
                title: $(obj).data("title"),
                width: $(obj).data("width"),
                height: $(obj).data("height"),
                init: initFun,
            });
        },
        search: function () {
            //表单
            var searchform = $("#searchForm").serializeArray();
            //表单转为Json
            var queryParams = page.getJson(searchform);

            productItemDG.datagrid({
                queryParams: queryParams
            });

            return false;
        },
        getJson: function (array) {
            if (array != undefined && array.length > 0) {
                var json = {};
                $(array).each(function (i, v) {
                    json[v.name] = v.value;
                });
                return json;
            } else {
                return {};
            }
        },
        queryParams:function(){
            var params = {
            };

            return params;
        },
        delete:function(){
            var row = permissionDG.datagrid('getSelected');
            if(row){
                var rowIndex = productItemDG.datagrid('getRowIndex', row);
                if(rowIndex<0){
                    window.top.$.messager.alert('提示', '请选择删除行.', 'info');
                }

                productItemDG.datagrid('deleteRow',rowIndex);
            }else{
                window.top.$.messager.alert('提示', '请选择删除行.', 'info');
                return;
            }
        },
        deleteRow:function(rowIndex){
            productItemDG.datagrid('deleteRow',rowIndex);
        },
        appendRow: function() {
            //新增一行
            var index = productItemDG.datagrid('appendRow', {
                partNo: '',
                productName:'',
                qty:'',
                lotNo:'',
                grade:'',
                remark:''
            }).datagrid("getRows").length - 1;
            //开始编辑
            productItemDG.datagrid('beginEdit',index);
            //编辑
            var editors = productItemDG.datagrid('getEditors', index);
            //获取物料
            var editor = editors[0];
            debugger;
            //验证是否为空对象
            if (editor != null) {
                $(editor.target).bind('keyup', function (e) {
		            var code = e.keyCode || e.which;
		            if (code == 13) {
                        var content = $(e.target).val();
                        Topro.UI.Ajax({
                            type: "POST",
                            url: "/StockIn/Explain",
                            data: {
                                content: content
                            },
                            success: function (res) {
                                if (res.success) {
                                    productItemDG.datagrid('updateRow', {index: index, row: {
                                        partNo: res.data.partNo,
                                        productName:res.data.productName,
                                        qty:res.data.qty,
                                        lotNo:res.data.lotNo,
                                        grade:res.data.grade,
                                        remark:''
                                    }});
                                } else {

                                }
                            },
                            error: function (json) {
                                return false;
                            }
                        });
		            }
	            });
            }
        },
        updateRow: function() {
            productItemDG.datagrid('updateRow', {index: 0, row: {
                partNo: '',
                productName:'2',
                qty:'',
                lotNo:'A0020.0107',
                grade:'',
                remark:''
            }});
        },
        save: function() {
            //获取所有数据
            var rows=productItemDG.datagrid('getRows');

            rows.forEach(function (row,index) {
                productItemDG.datagrid('endEdit',index);
            });
            //入库
            var stockIn = {};
            //订单号
            stockIn.orderId = $("input[name='orderId']").val();
            //高度
            stockIn.type = $("select[name='type']").find("option:selected").val();
            //容器编码
            stockIn.containerCode = $("input[name='containerCode']").val();
            //是否空容器
            stockIn.isContainer = $("input[name='isContainer']").is(':checked') == true ? @((int)IsContainer.空) : @((int)IsContainer.实);
            //库位
            stockIn.warehouseUnitId = $("input[name='warehouseUnitId']").val();
            //站台
            stockIn.stationNo = $("select[name='stationNo']").find("option:selected").val();
            //备注
            stockIn.remark = $("input[name='remark']").val();

            Topro.UI.Ajax({
                type: "POST",
                url: "/StockIn/Save",
                data: {
                    stockIn: stockIn,
                    inventoryItem: rows,
                },
                success: function (res) {
                    if (res.success) {
                        Topro.UI.Tip_success(res.message);
                        Topro.UI.DialogOpener().In.search();
                        Topro.UI.CloseDialog();
                        return false;
                    } else {
                        Topro.UI.Tip_danger("操作失败！" + res.message);
                        return false;
                    }
                },
                error: function (json) {
                    $("button[name='save']").attr("disabled", false);
                    Topro.UI.Tip_warning("操作失败！请查看网络，请重试。");
                    return false;
                }
            });
        },
        doSearch: function () {

            //固定行
            var columns = [
                [
                    {
                    field: 'partNo',
                    title: '料号',
                    width: 220,
                    align: 'left',
                    editor: {
                        type:'text',
                        options: {
                            onclick: function() {
                                alert('hello');
                            },
                            keyup: function() {
                                alert('hello');
                            }
                        }
                    }
                },
                {
                    field: 'productName',
                    title: '物料名称 ',
                    width: 170,
                    align: 'left',
                    editor: {
                        type:'text'
                    }
                },
                {
                    field: 'qty',
                    title: '数量',
                    width: 75,
                    align: 'center',
                    editor: {
                        type:'text'
                    }
                },
                {
                    field: 'lotNo',
                    title: '批号',
                    width: 150,
                    align: 'left',
                    editor: {
                        type:'text'
                    }
                },
                {
                    field: 'grade',
                    title: '等级',
                    width: 100,
                    align: 'center',
                    editor: {
                        type:'text'
                    }
                },
                {
                    field: 'remark',
                    title: '备注',
                    width: 100,
                    align: 'left',
                    editor: {
                        type:'validatebox'
                    }
                },
                {
                    field: 'id',
                    title: '操作',
                    width: 75,
                    align: 'center',
                    formatter: function (value, row, index) {
                        return "<a class=\"btn btn-danger btn-xs\" onclick=\"return page.deleteRow({0})\">删除</a>".format(index);
                    }
                }
                ]
            ];

            Topro.UI.DataGrid(productItemDG, {
                fit: true,
                queryParams: page.queryParams(),
                idField: 'id',
                pagination: false,
                singleSelect: true,
                rownumbers: true,
                showFooter: true,
                autoEditing:true,
                frozenColumns: null,
                columns: columns,
                onClickRow: function (index, row){
                   productItemDG.datagrid('endEdit',index);
                   productItemDG.datagrid('selectRow', index);
                   productItemDG.datagrid('beginEdit',index);
                }
            });
        }
    }

    $(function () {
        //搜索
        $(".search").click(function(){
            page.search();
        });

        jQuery(".warehouseUnit").click(function() { 
            //站台
            var type = $("select[name='type']").find("option:selected").val();

            if (Topro.Util.isNullOrEmptySpance(type)) {
                Topro.UI.Tip_warning("请先选择物料高度");
                return;
            }

            page.view(".warehouseUnit", `/Module/WarehouseUnit?type=${type}`);
        });

        page.doSearch();

        $("body").click( function() {
            //获取所有数据
            var rows=productItemDG.datagrid('getRows');

            rows.forEach(function (row,index) {
                //productItemDG.datagrid('endEdit',index);
            });
        });
    });
</script>